package com.ukao.cashregister.utils;

import android.content.Context;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.ukao.cashregister.listener.OnLocationListener;


/**
 * Created by Administrator on 2016/12/20.
 */

public class LocationUtils {

    public AMapLocationClient locationClient;
    private Context mActivity;
    private OnLocationListener mOnLocationListener;


    public void setLocationListener(OnLocationListener mOnLocationListener) {
        this.mOnLocationListener = mOnLocationListener;
    }


    public LocationUtils(Context mActivity,OnLocationListener  mOnLocationListener ) {
        this.mOnLocationListener = mOnLocationListener;
        this.mActivity = mActivity;
        locationClient = new AMapLocationClient(mActivity.getApplicationContext());
        //设置定位参数
        locationClient.setLocationOption(getDefaultOption());
        // 设置定位监听
        locationClient.setLocationListener(locationListener);


    }



    private AMapLocationClientOption getDefaultOption() {
        AMapLocationClientOption mOption = new AMapLocationClientOption();
       // mOption.setGpsFirst(true);
        mOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);//可选，设置定位模式，可选的模式有高精度、仅设备、仅网络。默认为高精度模式
        mOption.setHttpTimeOut(30000);//可选，设置网络请求超时时间。默认为30秒。在仅设备模式下无效
        mOption.setOnceLocation(true);//可选，设置是否单次定位。默认是false
        mOption.setOnceLocationLatest(true);//可选，设置是否等待wifi刷新，默认为false.如果设置为true,会自动变为单次定位，持续定位时不要使用
        AMapLocationClientOption.setLocationProtocol(AMapLocationClientOption.AMapLocationProtocol.HTTP);//可选， 设置网络请求的协议。可选HTTP或者HTTPS。默认为HTTP
        mOption.setSensorEnable(false);//可选，设置是否使用传感器。默认是false
        mOption.setWifiScan(true); //可选，设置是否开启wifi扫描。默认为true，如果设置为false会同时停止主动刷新，停止以后完全依赖于系统刷新，定位位置可能存在误差
        return mOption;
    }

    /**
     * 定位监听
     */
    AMapLocationListener locationListener = new AMapLocationListener() {
        @Override
        public void onLocationChanged(AMapLocation amapLocation) {
            if (amapLocation != null && amapLocation.getErrorCode() == 0) {

                if (mOnLocationListener != null)
                    mOnLocationListener.OnLocationSucceed(amapLocation);

            } else {
                if (mOnLocationListener != null)
                    mOnLocationListener.OnLocationFailure("定位失败");


            }
        }
    };

    /**
     * 开始定位
     *
     * @author hongming.wang
     * @since 2.8.0
     */
    public void startLocation() {
        locationClient.startLocation();
    }

    public void stopLocation() {
        // 停止定位
        locationClient.stopLocation();
    }

    public void destroyLocation() {
        if (null != locationClient) {
            /**
             * 如果AMapLocationClient是在当前Activity实例化的，
             * 在Activity的onDestroy中一定要执行AMapLocationClient的onDestroy
             */
            locationClient.onDestroy();
            locationClient = null;

        }
    }

}
